package m.jclecai.com.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.activity.EventActivity;
import m.jclecai.com.activity.LotteryEncyclopediasActivity;
import m.jclecai.com.activity.LotteryInfoActivity;
import m.jclecai.com.activity.LotteryLuckyActivity;
import m.jclecai.com.activity.LotteryTrendActivity;
import m.jclecai.com.activity.NewsActivity;
import m.jclecai.com.activity.NoticeActivity;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.Response.ResNewsList;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by ZD on 2017/7/21.
 * 发现  开关
 */

public class DiscoverSwitchFragment extends BaseFragment {


    @BindView(R.id.tv_notice)
    TextView mTvNotice;
    @BindView(R.id.rl_news)
    LinearLayout rl_news;



    @Override
    public int getLayoutId() {
        return R.layout.fragment_test_discover;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("发现");
    }

    @Override
    public void initData() {
        super.initData();
        if (MyApplication.status==0){
            rl_news.setVisibility(View.VISIBLE);
        }else{
            rl_news.setVisibility(View.GONE);
        }
        //这里我们要获取一条公告；
        requestData();
    }

    @OnClick(R.id.rl_active)
    public void onRlActiveClicked() {
        //活动中心
        startActivity(new Intent(getActivity(), EventActivity.class));
    }

    @OnClick(R.id.ll_lottery_encyclopedias)
    public void onLltteryEncylopedias() {
        //彩票百科
        startActivity(new Intent(getActivity(), LotteryEncyclopediasActivity.class));
    }

    @OnClick(R.id.ll_lottery_info)
    public void onLlLotteryInfoClicked() {
        //开奖信息
        startActivity(new Intent(getActivity(), LotteryInfoActivity.class));
    }


    @OnClick(R.id.rl_zoushi)
    public void onRlZoushiClicked() {
        //走势图
        startActivity(new Intent(getActivity(), LotteryTrendActivity.class));
    }

    @OnClick(R.id.rl_news)
    public void onRlNewsClicked() {
        //新闻资讯
        startActivity(NewsActivity.class);
    }

    @OnClick(R.id.ll_notice)
    public void onLlNoticeClicked() {
        //公告
        startActivity(new Intent(getActivity(), NoticeActivity.class));
    }

    public void startActivity(Class clazz) {
        startActivity(new Intent(getActivity(), clazz));
    }

    @OnClick(R.id.ll_lottery_lucky)
    public void onLotteryLuckyClicked() {
        Intent intent = new Intent(getActivity(), LotteryLuckyActivity.class);
        intent.putExtra("type", "xyxh");
        startActivity(intent);
    }


    private void requestData() {
        HashMap<String, String> params = new HashMap<>();
        Constant_Url.HTTP_URL = MyApplication.HTTP_URL;
        HttpManager.postAsyn(Constant_Url.NEWS_LIST, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                DisPlay("网络异常！！！");
            }

            @Override
            public void onResponse(String response) {

                if(!TextUtils.isEmpty(response)){

                    ResNewsList resNewsList = FastOk.get(response, ResNewsList.class);
                    if (resNewsList!=null && "0".equals(resNewsList.getStatus())) {

                        List<ResNewsList.NewsBean> rows = resNewsList.getRows();
                        if (rows.size()>0) {
                            mTvNotice.setText(rows.get(0).getTitle());
                        }
                    }else{
                        if(resNewsList!=null){
                            DisPlay(resNewsList.getMessage());
                        }else{
                            DisPlay("网络异常！！！");
                        }
                    }

                }else{
                    //这里表示返回为空；
                    DisPlay("网络异常！！！");
                }

            }
        },params);
    }


}
